package com.company.statistician;


import com.company.runner.DataSet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Column {
    int beginAt;
    double value;
    int endAt;

    public Column(int beginAt, int barrelSize, double value) {
        this.beginAt = beginAt;
        this.value = value;
        this.endAt = beginAt + barrelSize;
    }

    public static void showValues(ArrayList<Column> columns, String path) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("datasets");
        for (int i = 0; i < columns.size(); i++) {
            double befVal = columns.get(i).getValue();
            XSSFRow row = sheet.createRow(i);
            XSSFCell befCell = row.createCell(0);
            befCell.setCellValue(befVal);
        }
        try {
            workbook.write(new FileOutputStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void showValues(ArrayList<Column> columns, XSSFSheet sheet, int colIndex) {
        for (int i = 0; i < columns.size(); i++) {
            double befVal = columns.get(i).getValue();
            XSSFRow row = sheet.getRow(i);
            if (row == null)
                row = sheet.createRow(i);
            XSSFCell befCell = row.createCell(colIndex);
            befCell.setCellValue(befVal);
        }
    }

    @Override
    public String toString() {
        return beginAt + "\t" + value;
    }

    public double getValue() {
        return value;
    }

    public int getBeginAt() {
        return beginAt;
    }

    public int getEndAt() {
        return endAt;
    }

    public void setValue(double newValue) {
        this.value = newValue;
    }
}
